import filterData from '@/common/filterData';
import mergeData from '@/common/mergeData';
import getDepartment from '@/common/getDepartment';
const { departOrder } = getDepartment;

const base = { title: '智家业务在途情况' }
const dataset = {
  source: [
    ['日期', '在途', '超长', '预警', 'VIP'],
    ['茂南', 186, 100, 179, 43]
  ]
};

function getChartData([dataPool1], condition) {
  const defaultCondition = ['茂南', '电白', '高州', '化州', '信宜'];

  const hasCondition = condition && condition.length && (condition[0].length || condition[1].length);
  const regionCondition = hasCondition ? [...condition[0]] : defaultCondition;
  const gridCondition = hasCondition ? [...condition[1]] : [];

  const biz = ['all', 'FTTR', 'zuwang', 'kanjia', 'mensuo', 'pinganxiangcun'];
  const SheetHeader = ['总体', 'FTTR', '组网', '看家', '门锁', '平安乡村'];
  const collection = dataPool1['xinyewu_zt'];

  const sheets = biz.map((biz, bizIndex) => {
    const data = collection[biz];

    const maxDate = Math.max(...data.map(i => i.date))
    const lastDay = data.filter(i => i.date === maxDate)
    let regionData = [], gridData = [];
    if (regionCondition.length) {
      regionData = filterData.countByField('region', lastDay, regionCondition, true);
    }
    if (gridCondition.length) {
      gridData = filterData.countByField('grid', lastDay, gridCondition, true);
    }
    const rgdata = regionData.concat(gridData).sort((a, b) => departOrder[a[0]] - departOrder[b[0]])
    const merge = [
      ['地区', SheetHeader[bizIndex]]
    ].concat(rgdata);
    return merge;
  });

  const data = mergeData.mergeSheets(sheets);
  this.dataset.source = data;

  // 非日期类
  // if (data.length > 7) {
  //   this.dataZoom[0].show = true;
  //   this.dataZoom[0].type = 'inside'
  // }

  // 返回数据到页面缓存
  return data

}

const seriesTemplate = [['bar', 'bar', 'bar', 'bar', 'bar', 'bar']];

// ======更新内容↑========

const series = seriesTemplate.flatMap((arr, index) =>
  arr.map(type => ({ type, yAxisIndex: index }))
);

export default {
  title: {
    text: base.title,
    left: 'center'
  },
  legend: {
    type: 'scroll',
    top: 30
  },
  tooltip: {
    trigger: 'axis',
    axisPointer: {
      type: 'cross',
      crossStyle: {
        color: '#999'
      }
    }
  },
  grid: {
    width: '96%',
    height: '80%',
    left: '2%',
    right: '2%',
    top: '16%',
    bottom: '2%',
    containLabel: true
  },
  dataset,
  dataZoom: [{ show: false }],
  xAxis: {
    type: 'category',
    axisPointer: {
      type: 'shadow'
    }
  },
  yAxis: [
    {
      type: 'value',
      name: ''

    }, {
      type: 'value',
      name: '',
      axisLabel: {
        formatter: (value) => `${value}`
      }
    }
  ],
  series,
  getChartData,
}